﻿@model Century.Model.ViewModel.FileExplorerViewModel
@using System.IO
@{
    Layout = null;
}
@{
    DirectoryInfo Directory = new DirectoryInfo(Server.MapPath(Model.Path));
    string randomName = Model.ExplorerID;
    string currentPath = Model.Path.Replace("~", string.Empty);
    string basePath = "";//Century.Common.Config.ConfManager.Instance.FileExplorer.DefaultPath.ConfigValue.Replace("~", string.Empty);
}
<div class="file_explorer">
    @{
        int i = 1;
        int groupsize = 7;
        string fileIconPath = Url.Content("~/Content/themes/admin/img/general/file.png");
        string fileIcon = string.Empty;
        foreach (FileInfo file in Directory.GetFiles().ToList())
        {
            <text>
                @{
                    fileIcon = fileIconPath;
                    if ((new List<string> { ".jpg", ".jpeg", ".gif", ".png" }).ToList().Any(f => f == file.Extension))
                    {
                        fileIcon = currentPath + "/" + file.Name;
                    }
                }
                <a href="javascript:void(0);" class="lnk">
                    <img src="@(fileIcon)" alt="@(file.Name)" width="100" height="100" onclick="onSelectImage(this, '@(randomName)', '@(currentPath + "/" + file.Name)');"/>
                    <div class="property">@(file.Name)</div>
                </a>
            </text>
                if (i % groupsize == 0)
                {
                    <text><br /></text>
                }
                i++;
        }
    }
    <input id="hiddenSelectedImage_@(randomName)" type="hidden" value="@(basePath)/page_header_default.jpg" />
</div>
<div class="file_viewer">
    <br />
    <img alt="Preview" id="preview_@(randomName)" src="@(basePath)/page_header_default.jpg" />
    <br />
    <div id="previewed_name_@(randomName)"></div>
    <br />
    <input id="btnSelectImage_@(randomName)" type="button" value="Select" class="select" />
</div>
<script language="javascript" type="text/javascript">
    function onSelectImage(obj, key, path) {
        $(".bordered").removeClass("bordered");
        $(obj).addClass("bordered");
        var previewImg = $('#preview_' + key);
        var previewedImgName = $('#previewed_name_' + key);
        if (path.indexOf('jpg') > 0 || path.indexOf('jpeg') > 0 || path.indexOf('png') > 0) {
            previewImg.attr("src", path);
        }
        else {
            previewImg.attr("src", '@(fileIconPath)');
        }

        $(previewedImgName).html($(obj).attr("alt"));
        var hidden = $('#hiddenSelectedImage_' + key);
        hidden.val(path);
    }
</script>
